﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using ConfigurationManager.View.Dialog;

namespace ConfigurationManager.View
{
    public partial class frmInstanceList : Form
    {
        List<Model.Instance> _instances;
        List<Model.Product> _products;
        public frmInstanceList()
        {
            InitializeComponent();
            dtgInstances.AutoGenerateColumns = false;
            this.Load += new EventHandler(frmInstanceList_Load);
        }

        void frmInstanceList_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
            LoadInstances();

        }

        private void LoadAuxData()
        {
            //recuperar os produtos cadastrados
            _products = Model.Product.Retrieve();
        }

        private void LoadInstances()
        {
            LoadAuxData();
            _instances = Model.Instance.Retrieve();
            dtgInstances.DataSource = _instances;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadInstances();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            InsertInstance();
        }

        private void dtgInstances_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;

            if (dtgInstances.Rows[e.RowIndex].Cells[e.ColumnIndex] == dtgInstances.Rows[e.RowIndex].Cells["Edit"])
                EditInstance((Model.Instance)dtgInstances.Rows[e.RowIndex].DataBoundItem);
            else if (dtgInstances.Rows[e.RowIndex].Cells[e.ColumnIndex] == dtgInstances.Rows[e.RowIndex].Cells["Delete"])
                DeleteInstance((Model.Instance)dtgInstances.Rows[e.RowIndex].DataBoundItem);
        }

        private void dtgInstances_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            EditInstance((Model.Instance)dtgInstances.Rows[e.RowIndex].DataBoundItem);
        }

        private void DeleteInstance(ConfigurationManager.Model.Instance instance)
        {
            try
            {
                int id;
                string name;
                id = instance.Id;
                name = instance.Name;
                if (MessageBox.Show(string.Format("Confirma a exclusão da instância {0} - {1}?", id, name), "Confimar Exclusão", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Model.Instance.Delete(id);
                    MessageBox.Show("Instância excluída com sucesso!", "Process Manager Configuration", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadInstances();
                }
            }
            catch (ValidationException ex)
            {
                MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                //EventLog.WriteEntry("Process Manager - Configuration Manager", ex.ToString(), EventLogEntryType.Error);
                //MessageBox.Show("Ocorreu um erro interno, verifique o event viewer para maiores detalhes");
                Program.HandleException(ex);
            }
        }

        private void EditInstance(ConfigurationManager.Model.Instance instance)
        {
            LoadAuxData();

            frmInstance addProduct = new frmInstance(_products, instance);
            addProduct.Owner = this;
            if (addProduct.ShowDialog() == DialogResult.OK)
                LoadInstances();
        }

        private void InsertInstance()
        {
            LoadAuxData();

            frmInstance addProduct = new frmInstance(_products);
            addProduct.Owner = this;
            if (addProduct.ShowDialog() == DialogResult.OK)
                LoadInstances();
        }
    }
}
